/**
 ******************************************************************************
 * @file    watermark.c
 * @author  Chavis.Chen
 * @version V1.0.0
 * @date    26-Sep-2018
 * @brief   This file contains the abstraction of linklist_node_t.
 ******************************************************************************
 *
 *  The MIT License
 *  Copyright (c) 2018 Chavis.Chen.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is furnished
 *  to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
 *  IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 */


#include "debug.h"
#include "watermark.h"
#include <stdlib.h>

#if 1
#define wm_log(msg, ...)	custom_log("WM", msg, ##__VA_ARGS__)
#else
#define wm_log(msg, ...)
#endif


static OSStatus wm_lock(wm_handle_t * wm_handle_ptr)
{
	OSStatus 	err = kNoErr;
	wm_t *		wm_ptr = NULL;

	require_action(wm_handle_ptr && *wm_handle_ptr, exit, err = kParamErr);

	wm_ptr = * wm_handle_ptr;

	err = pthread_mutex_lock(&wm_ptr->mutex);
	require_noerr(err, exit);

exit:
	return err;
}

static OSStatus wm_unlock(wm_handle_t * wm_handle_ptr)
{
	OSStatus 	err = kNoErr;
	wm_t * 		wm_ptr = NULL;

	require_action(wm_handle_ptr && *wm_handle_ptr, exit, err = kParamErr);

	wm_ptr = * wm_handle_ptr;

	err = pthread_mutex_unlock(&wm_ptr->mutex);
	require_noerr(err, exit);

exit:
	return err;
}

OSStatus wm_init(wm_handle_t * wm_handle_ptr, uint32 bufSz, uint32 lowVal, uint32 highVal, void (* cb)(wm_event_e, void *))
{
	OSStatus 	err = kNoErr;
	wm_t *		wm_ptr = NULL;

	require_action(wm_handle_ptr && bufSz, exit, err = kParamErr);

	wm_ptr = malloc(sizeof(wm_t));
	require_action(wm_ptr, exit, err = kNoMemoryErr);

	memset(wm_ptr, 0, sizeof(wm_t));

	err = ring_buffer_init(&wm_ptr->ringBuffer, bufSz);
	require_noerr(err, exit);

	err = pthread_mutex_init(&wm_ptr->mutex, NULL);
	require_noerr(err, exit);

	wm_ptr->lowVal = lowVal;
	wm_ptr->highVal = highVal;
	wm_ptr->isReachedLow = FALSE;
	wm_ptr->isReachedHigh = FALSE;
	wm_ptr->cb = cb;
	
	* wm_handle_ptr = wm_ptr;

exit:
	if(err != kNoErr)
	{
		if(wm_ptr)
			free(wm_ptr);

		if(wm_ptr->ringBuffer)
			ring_buffer_deinit(&wm_ptr->ringBuffer);
	}
	return err;
}

OSStatus wm_deinit(wm_handle_t * wm_handle_ptr)
{
	OSStatus 	err = kNoErr;
	wm_t * 		wm_ptr = NULL;

	require_action(wm_handle_ptr && *wm_handle_ptr, exit, err = kParamErr);

	wm_ptr = * wm_handle_ptr;

	err = ring_buffer_deinit(&wm_ptr->ringBuffer);
	require_noerr(err, exit);

	err = pthread_mutex_destroy(&wm_ptr->mutex);
	require_noerr(err, exit);

	free(wm_ptr);

	* wm_handle_ptr = NULL;

exit:
	return err;
}

OSStatus wm_read(wm_handle_t * wm_handle_ptr, uint8 * buf, uint32 toReadLen, uint32 * pActualReadLen, void * lowCbArgv, void * emptyCbArgv)
{
	OSStatus 	err = kNoErr;
	wm_t * 		wm_ptr = NULL;
	uint32		ring_buffer_used_size = 0;
	bool		is_locked = FALSE;

	require_action(wm_handle_ptr && *wm_handle_ptr && buf, exit, err = kParamErr);

	wm_ptr = * wm_handle_ptr;

	err = wm_lock(wm_handle_ptr);
	require_noerr(err, exit);

	is_locked = TRUE;

	err = ring_buffer_get_used_size(&wm_ptr->ringBuffer, &ring_buffer_used_size);
	require_noerr(err, exit);

	if(ring_buffer_used_size <= wm_ptr->lowVal)
	{
		wm_ptr->isReachedLow = TRUE;
		if(wm_ptr->cb)
			wm_ptr->cb(WM_EVENT_LOW, lowCbArgv);
	}

	err = ring_buffer_read_ex(&wm_ptr->ringBuffer, buf, toReadLen, pActualReadLen);
	require_noerr(err, exit);

	err = ring_buffer_get_used_size(&wm_ptr->ringBuffer, &ring_buffer_used_size);
	require_noerr(err, exit);

	if(ring_buffer_used_size <= wm_ptr->lowVal)
	{
		if(wm_ptr->isReachedLow == FALSE)
		{
			wm_ptr->isReachedLow = TRUE;
			if(wm_ptr->cb)
				wm_ptr->cb(WM_EVENT_LOW, lowCbArgv);
		}
	}

	if(ring_buffer_used_size < wm_ptr->highVal)
		wm_ptr->isReachedHigh = FALSE;

	if(ring_buffer_used_size == 0)
	{
		if(wm_ptr->cb)
			wm_ptr->cb(WM_EVENT_EMPTY, emptyCbArgv);
	}

exit:
	if(is_locked)
		wm_unlock(wm_handle_ptr);

	return err;
}


OSStatus wm_write(wm_handle_t * wm_handle_ptr, uint8 * buf, uint32 toWriteLen, uint32 * pActualWriteLen, void * highCBArgv)
{
	OSStatus 	err = kNoErr;
	wm_t * 		wm_ptr = NULL;
	uint32		ring_buffer_used_size = 0;
	bool		is_locked = FALSE;

	require_action(wm_handle_ptr && *wm_handle_ptr && buf, exit, err = kParamErr);

	wm_ptr = * wm_handle_ptr;

	err = wm_lock(wm_handle_ptr);
	require_noerr(err, exit);

	is_locked = TRUE;

	err = ring_buffer_get_used_size(&wm_ptr->ringBuffer, &ring_buffer_used_size);
	require_noerr(err, exit);

	if(ring_buffer_used_size >= wm_ptr->highVal)
	{
		wm_ptr->isReachedHigh = TRUE;
		if(wm_ptr->cb)
			wm_ptr->cb(WM_EVENT_HIGH, highCBArgv);
	}

	err = ring_buffer_write_ex(&wm_ptr->ringBuffer, buf, toWriteLen, pActualWriteLen);
	require_noerr(err, exit);

	err = ring_buffer_get_used_size(&wm_ptr->ringBuffer, &ring_buffer_used_size);
	require_noerr(err, exit);

	if(ring_buffer_used_size >= wm_ptr->highVal)
	{
		if(wm_ptr->isReachedHigh == FALSE)
		{
			wm_ptr->isReachedHigh = TRUE;
			if(wm_ptr->cb)
				wm_ptr->cb(WM_EVENT_HIGH, highCBArgv);
		}
	}

	if(ring_buffer_used_size > wm_ptr->lowVal)
		wm_ptr->isReachedLow = FALSE;

exit:
	if(is_locked)
		wm_unlock(wm_handle_ptr);

	return err;
}

OSStatus wm_cnt(wm_handle_t * wm_handle_ptr, uint32 * pWmCnt)
{
	OSStatus 	err = kNoErr;
	wm_t * 		wm_ptr = NULL;
	bool		is_locked = FALSE;

	require_action(wm_handle_ptr && *wm_handle_ptr && pWmCnt, exit, err = kParamErr);

	wm_ptr = * wm_handle_ptr;

	err = wm_lock(wm_handle_ptr);
	require_noerr(err, exit);

	is_locked = TRUE;

	err = ring_buffer_get_used_size(&wm_ptr->ringBuffer, pWmCnt);
	require_noerr(err, exit);

exit:
	if(is_locked)
		wm_unlock(wm_handle_ptr);

	return err;
}